
export const listCtrl = {
  watch: {
    list: {
      immediate: true,
      handler(v) {
        this._initSelfList(v)
      }
    }
  },
  data() {
    return {
      self_id: 1, // 列表内部维护的id
      self_list: null, // 内部列表
    }
  },
  methods: {
    _initSelfList(v) {
      if (!v) {
        this.self_list = null
      } else {
        let _v = [].concat(v)
        _v.sort((a, b) => {
          return a.sortNo - b.sortNo
        })
        this.self_list = this._wrapFileInfo(_v)
      }
    },
    isDisableField(row, prop) {
      const disableField = this.disableFieldMap[row.mediaType]
      if (!disableField) return false
      return disableField.indexOf(prop) > -1
    },
    // 秒转[HH, mm, ss]
    _s2Hms(s) {
      const oneDay = 60 * 60 * 24
      if (s >= oneDay) s -= oneDay
      let m = ~~(s / 60)
      if (m < 1) {
        return ['00', '00', `${s > 9 ? s : '0' + s}`]
      } else {
        s = s % 60
        const H = ~~(m / 60)
        if (H < 1) {
          return ['00', `${m > 9 ? m : '0' + m}`, `${s > 9 ? s : '0' + s}`]
        } else {
          m = m % 60
          return [`${H > 9 ? H : '0' + H}`, `${m > 9 ? m : '0' + m}`, `${s > 9 ? s : '0' + s}`]
        }
      }
    },
    _Hms2s(hms) {
      const [H, m, s] = hms
      return (Number(H) * 60 * 60) + (Number(m) * 60) + Number(s)
    },

    // 将节目信息包装成列表所需
    _wrapFileInfo(info) {
      if (!info) return info
      if (info instanceof Array) {
        return info.map(element => {
          return this._wrapFileInfo(element)
        });
      }
      let out
      if (info.programId) { // 暂时用programId是否存在来区分此信息是否是后台来的数据
        out = {
          ...info,
          startTime: info.startTime.split(':'),
          endTime: info.endTime.split(':')
        }
      } else {
        out = {
          programId: info.id,
          programName: info.name,
          startTime: ['00', '00', '00'],
          endTime: ['23', '59', '59'],
          priority: 1
        }
      }
      return this._wrapSelfId(out)
    },
    // 给信息添加self_id
    _wrapSelfId(info) {
      if(info.argus) {
        const json = info.argus
        info = {
          ...info,
          ...json
        }
      }
      return {
        ...info,
        _id: this.self_id++
      }
    },
    // 新增行
    addRow(info) {
      if (!info) return this.$alert('提示', '请选择要添加的节目')
      if (info instanceof Array) {
        this.self_list.push(...this._wrapFileInfo(info))
      } else {
        this.self_list.push(this._wrapFileInfo(info))
      }
    },
    // 移除行
    removeRow(row) {
      if (!row) return this.$alert('提示', '请选择要移除的节目')
      if (row instanceof Array) {
        let ids = row.map(item => item._id)
        this.self_list = this.self_list.filter(item => {
          return ids.indexOf(item._id) === -1
        })
      } else {
        let rowindex = this.self_list.findIndex(item => item._id === row._id)
        if (rowindex > -1) {
          this.self_list.splice(rowindex, 1)
        }
      }
    },
    // 替换行
    async replaceRow(lrow, rrow) {
      if(!lrow && !rrow) return this.$alert('提示', '请同时选中播放编辑列表和播放列表的节目进行替换')
      if(!lrow) return this.$alert('提示', '请选中播放编辑列表的一个节目进行替换')
      if(!rrow) return this.$alert('提示', '请选中播放列表的节目进行替换')
      if (lrow instanceof Array) return this.$alert('提示', '节目不支持多选')
      const bool = await this.$confirm('提示', `是否确认替换节目`)
      if(!bool) {
        return
      }
      let propramArray = []
      if(rrow instanceof Array) {
        propramArray = rrow
      } else {
        propramArray.push(rrow)
      }
      propramArray.forEach(proitem => {
        let rowindex = this.self_list.findIndex(item => item._id === proitem._id)
        if (rowindex > -1) {
          const nrrow = proitem
          if(nrrow.argus) {
            nrrow.argus.programName = lrow.name
          } else {
            nrrow.programName = lrow.name
          }
          const nrow = {
            ...nrrow,
            programName: lrow.name,
            programId: lrow.id
          }
          this.self_list.splice(rowindex, 1, nrow)
        }
      })
    },
    // 上移行
    upRow(row) {
      if (row instanceof Array) return this.$alert('提示', '此操作不支持多选')
      let rowindex = this.self_list.findIndex(item => item._id === row._id)
      if (rowindex <= 0) return
      let _copyList = [].concat(this.self_list)
      let temp = _copyList[rowindex - 1]
      _copyList[rowindex - 1] = _copyList[rowindex]
      _copyList[rowindex] = temp
      this.self_list = _copyList
      this.$nextTick(() => {
        this.$refs.list.$refs.list.toggleRowSelection(_copyList[rowindex - 1], true)
      })
    },
    // 下移行
    downRow(row) {
      if (row instanceof Array) return this.$alert('提示', '此操作不支持多选')
      let rowindex = this.self_list.findIndex(item => item._id === row._id)
      if (rowindex < 0 || rowindex === (this.self_list.length - 1)) return
      let _copyList = [].concat(this.self_list)
      let temp = _copyList[rowindex + 1]
      _copyList[rowindex + 1] = _copyList[rowindex]
      _copyList[rowindex] = temp
      this.self_list = _copyList
      this.$nextTick(() => {
        this.$refs.list.$refs.list.toggleRowSelection(_copyList[rowindex + 1], true)
      })
    }
  }
}
